﻿//https://leetcode.cn/problems/jump-game-ii/?envType=study-plan-v2&envId=top-100-liked

class Solution {
public:
    int jump(vector<int>& nums)
    {
        int n = nums.size();
        int left = 0, right = 0, ret = 0, maxpos = 0;

        while (left <= right)
        {
            if (maxpos >= n - 1)
            {
                return ret;
            }

            for (int i = left; i <= right; i++)
            {
                maxpos = max(maxpos, nums[i] + i);
            }
            left = right + 1;
            right = maxpos;
            ret++;
        }

        return ret;
    }
};

// https://leetcode.cn/problems/partition-labels/submissions/618468917/?envType=study-plan-v2&envId=top-100-liked

class Solution {
public:
    vector<int> partitionLabels(string s)
    {
        vector<int> ret;
        int n = s.size();
        int last[26] = { 0 };
        for (int i = 0; i < n; i++)
        {
            last[s[i] - 'a'] = i;
        }

        int star = 0, end = 0;
        for (int i = 0; i < n; i++)
        {
            end = max(end, last[s[i] - 'a']);
            if (end == i)
            {
                ret.push_back(end - star + 1);
                star = end + 1;
            }
        }

        return ret;
    }
};

//https://leetcode.cn/problems/climbing-stairs/submissions/618470032/?envType=study-plan-v2&envId=top-100-liked

class Solution {
public:
    int climbStairs(int n)
    {
        int p = 0, q = 0, r = 1;
        for (int i = 1; i <= n; i++)
        {
            p = q;
            q = r;
            r = p + q;
        }

        return r;
    }
};
